package com.lx.pc.service.user.impl;

import java.math.BigInteger;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lx.pc.domain.entity.user.UserEntity;
import com.lx.pc.domain.vo.user.UserVo;
import com.lx.pc.mybatis.repository.user.UserRepository;
import com.lx.pc.redis.SpringRedisTemplate;
import com.lx.pc.service.AbstractService;
import com.lx.pc.service.RedisKeyGenerator;
import com.lx.pc.service.user.IUserService;

@Service("userService")
public class UserServiceImpl extends AbstractService<UserEntity, UserVo, BigInteger, UserRepository> implements IUserService{
	
	//@Resource(name = "springRedisTemplate")
	//private SpringRedisTemplate springRedisTemplate;
	
	@Override
	public UserVo findByUsername(UserVo user) {
		return  etoV(repository.findByUsername(user));
	}
	
	/*public UserVo  findByUsername(UserVo user) {
		String userInfoKey = RedisKeyGenerator.getInstance().getUserInfoKey(user.getId().toString());
		if (springRedisTemplate.exists(userInfoKey)) {
			user = (UserVo)springRedisTemplate.read(userInfoKey);
		} else {
			user = etoV(repository.findByUsername(user));
			springRedisTemplate.save(userInfoKey, user);
		}
		return  user;
	}*/
	
	
	@Override
	public UserVo findByUserOpenid(UserVo user) {

		return etoV(repository.findByUserOpenid(user));
	}

	@Override
	public UserVo findByUserId(BigInteger userId) {
		 return etoV(repository.findByUserId(userId));
		
	}

	@Override
	public String saveUser(UserVo user) {
		if (user == null) {
			return null;
		}
		try {
			this.save(user);
			/*String userInfoKey = RedisKeyGenerator.getInstance().getUserInfoKey(user.getId().toString());
			if (springRedisTemplate.exists(userInfoKey)) {
				springRedisTemplate.delete(userInfoKey);
			}
			springRedisTemplate.save(userInfoKey, user);*/
			return user.getId().toString();
		} catch (Exception e) {
			logger.error("v:" + user.toString(), e);
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String updateUser(UserVo user) {
		repository.update(user);
		return user.getId().toString();
	}

	@Override
	public List<UserVo> findByUser(UserVo user) {
		return etoV(repository.findByUser(user));
	}

	@Override
	public List<UserVo> findByUserLatLng(UserVo user) {

		return etoV(repository.findByUserLatLng(user));
	}


}


